package crudfx.field;
import crudfx.container.*;
import crudfx.control.*;

import javax.swing.border.*;
import crudfx.data.*;
import crudfx.interop.*;
import javax.swing.*;
import java.awt.event.*;
import javafx.scene.paint.*;
import java.text.*;
import java.lang.*;
import java.util.*;

/**
 * @profile desktop
 * @author Sergey Surikov
 */
public class ArtNumberField extends UIComponent{
    var component:JamRight;
    var lock=false;
    var formatter:DecimalFormat=new DecimalFormat("#");
    public var upRollover:javax.swing.Icon;
    public var up:javax.swing.Icon;
    public var downRollover:javax.swing.Icon;
    public var down:javax.swing.Icon;
    public var value:Number=0 on replace {
        if(not lock){
            lock=true;
            textValue=formatter.format(value);
            lock=false;
            }
        };
    public var stepSize:Number=0.1;
    public var min:Number=-1000.0;
    public var max:Number=1000.0;
    public-init var bold:Boolean=false;
    public-init var italic:Boolean=false;
    public-init var serifs:Boolean=false;
    public-init var monospaced:Boolean=false;
    public-init var size:Number=9;
    public var color:Color;
    public var selectionColor:Color;
    public var selectionBackground:Color;
    public var afterDot:Number=0 on replace{
        var pattern="0";
        if(afterDot>0){
            pattern="{pattern}.";
            for(i in [1 .. afterDot]){
                pattern="{pattern}0";
                }
            }
        formatter=new DecimalFormat(pattern);
        };
    var upButton:ArtButton;
    var downButton:ArtButton;
    var textValue:String on replace {
        if(not lock){
            lock=true;
            var numberFormat=NumberFormat.getNumberInstance(Locale.ENGLISH);
            try{
                value=numberFormat.parse(textValue.replaceAll(",","\\.")).doubleValue();
                //value.parseFloat(textValue);
            }catch(x){
                //ignore
                }
            textValue=formatter.format(value);
            value=numberFormat.parse(textValue.replaceAll(",","\\.")).doubleValue();
            lock=false;
            }
        }
    init{
        //textValue="{value}";
        upButton=ArtButton{
            icon:bind up
            rollover:bind upRollover
            action:function():Void{
                if(value < max){
                    value=value+stepSize;
                }else {
                    value = max;
                }
                }
            }
        downButton=ArtButton{
            icon:bind down
            rollover:bind downRollover
            action:function():Void{
                if(value > min){
                    value=value-stepSize;
                }else {
                    value = min;
                }
                }
            }
        component=JamRight{
            content:ArtStringField{
                size:size
                bold:bold
                italic:italic
                serifs:serifs
                monospaced:monospaced
                text:bind textValue with inverse
                color:bind color
                selectionColor:bind selectionColor
                selectionBackground:bind selectionBackground
                }
            jam:JamTop{
                jam:upButton
                content:JamTop{
                    jam:downButton
                    }
                }
            }
            lock=true;
            textValue=formatter.format(value);
            lock=false;
        }
    override public function getJComponent():JComponent{
        return component.getJComponent();
        }
    override public function highlight(s:String):Boolean{
        return false;
        }
    override public function gather():DataTree{
        var me:DataTree=DataTree{ name:"ArtNumberField" };
        me.node("value").asNumber(0).value=value;
        return me;
        }
    }
